//
//  VisitorLoginView.swift
//  WeiBo
//
//  Created by 史亚洲 on 16/1/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//导入SnapKit命名空间
import SnapKit

//1.声明协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {

    //协议方法
    optional func userWillRegister()
    
    func userWillLogin()

}


class VisitorLoginView: UIView {
    
    //2.协议属性
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    
    //按钮的监听事件方法
    @objc private func loginBtnClick() {
        
        visitorViewDelegate?.userWillLogin()
    
    
    }
    
    @objc private func registerBtnClick() {
        
    visitorViewDelegate?.userWillRegister?()
    
    }
    
    
    //外部调用的重要方法
    func setupUIWithInfo(imageName: String?,tipText: String) {
    
        if let img = imageName {
            
            circleIcon.image = UIImage(named: img)
        
        //隐藏大房子
        largeHouse.hidden = true
        bringSubviewToFront(circleIcon)
      
        
        }else{
            
            //首页在运动,有动画效果
            startAnimation()
        
        
        }
    tipLabel.text = tipText
    
    
}
    //在首页圆圈有动画效果
    func startAnimation() {
        
        //1.基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
        //2.设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        
        
        //3.持续时间
        base.duration = 10
        
        //4.设置动画完成/页面失去活跃状态,不移除图层,不设置的话,切换后动画停止
        base.removedOnCompletion = false
        
        //5.添加动画
        circleIcon.layer.addAnimation(base, forKey: nil)
        
    
    
    
}
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置UI
        setupUI()
    }

    //默认报错,当程序员通过sb/xib加载该视图,程序会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  private  func setupUI() {
    //添加子视图
    addSubview(circleIcon)
    //需要把圆圈盖住
    addSubview(backView)
    addSubview(largeHouse)
    addSubview(tipLabel)
    addSubview(loginBtn)
    addSubview(registerBtn)
    
    
    //设置自动布局,苹果手动代码布局,通过VFL NSLayoutConstraint
        /*
        1.item: 表示要设置约束的对象
        2.attribute: 需要添加约束的属性
        3.relatedBy: 参照方法 一般使用 精准参照 .Equal\
        4.toItem: 约束被参照的对象
        attribute:被参照约束的属性
        
        view1.attr1 = view2.attr2 * multiplier + constant
        */
        //将约束对象添加到 父视图上
        //注意一点 自动布局能够生效 需要屏蔽frame布局方式
//    circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
    /*
    添加约束 推荐 使用链式约束
    */
    //snp_makeConstraints 表示添加一条约束
    circleIcon.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY).offset(-80)
    }
    largeHouse.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(circleIcon.snp_center)
        
    }
    tipLabel.snp_makeConstraints { (make) -> Void in

        make.centerX.equalTo(circleIcon)
        make.top.equalTo(circleIcon.snp_bottom).offset(20)
        make.width.equalTo(220)
        
        //为了防止页面跳动
        make.height.equalTo(60)
    }
    loginBtn.snp_makeConstraints { (make) -> Void in
        //make.centerX.equalTo(tipLabel)
        make.left.equalTo(tipLabel.snp_left)
        make.top.equalTo(tipLabel.snp_bottom).offset(20)
        make.width.equalTo(100)
    }
    registerBtn.snp_makeConstraints { (make) -> Void in
        make.right.equalTo(tipLabel.snp_right)
        make.top.equalTo(tipLabel.snp_bottom).offset(20)
        make.width.equalTo(100)
    }

    backView.snp_makeConstraints { (make) -> Void in
        make.top.left.right.equalTo(self)
        make.bottom.equalTo(registerBtn.snp_bottom)
    }
    self.backgroundColor = UIColor(white: 0.93, alpha: 1)
    
    //3.添加监听事件
    loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
    registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
    
    
}

    
    //1.懒加载获取所有的子视图
    //1.1背景图片
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //1.2圆圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //1.3房子
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //1.4提示文案
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        
        l.font = UIFont.systemFontOfSize(15)
        l.textAlignment = .Center
        l.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        l.numberOfLines = 0
        l.textColor = UIColor.darkGrayColor()
        l.sizeToFit()
        return l
 
    }()
    
    //1.5登录按钮
    lazy var loginBtn: UIButton = {
    
        let btn = UIButton()
        //设置按钮文字
        btn.setTitle("登录", forState: .Normal)
        //设置字体
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
 
        
    }()
    
    
    //1.6注册按钮
    lazy var registerBtn: UIButton = {
    
        let btn = UIButton()
        
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        let image = UIImage(named: "common_button_white_disable")
        let w = Int((image?.size.width)! / 2)
        let h = Int((image?.size.height)! / 2)
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth( w , topCapHeight: h), forState: .Normal)
        return btn
    
    }()
    
    
    
}
